package sensor.demo;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class Sensors extends Activity implements SensorEventListener {
   
	private SensorManager sm;
	private Sensor accelerometer;
	private Sensor compass;
	private Sensor light;
	private Sensor orientation;
	
	private TextView xViewA = null;
	private TextView yViewA = null;
	private TextView zViewA = null;
	private TextView xViewO = null;
	private TextView yViewO = null;
	private TextView zViewO = null;
	private TextView xViewC = null;
	private TextView yViewC = null;
	private TextView zViewC = null;
	private TextView vLight = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	sm = (SensorManager)getSystemService(SENSOR_SERVICE);
    	accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    	compass = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    	light = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
    	orientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    	setContentView(R.layout.main);
    	xViewA = (TextView) findViewById(R.id.accelX);
        yViewA = (TextView) findViewById(R.id.accelY);
        zViewA = (TextView) findViewById(R.id.accelZ);
        xViewC = (TextView) findViewById(R.id.compX);
        yViewC = (TextView) findViewById(R.id.compY);
        zViewC = (TextView) findViewById(R.id.compZ);
        xViewO = (TextView) findViewById(R.id.orientX);
        yViewO = (TextView) findViewById(R.id.orientY);
        zViewO = (TextView) findViewById(R.id.orientZ);
        vLight = (TextView) findViewById(R.id.light);
    	
    }

    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, compass, SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, light, SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, orientation, SensorManager.SENSOR_DELAY_NORMAL);
        
    }

    @Override
    protected void onStop() {
    	sm.unregisterListener(this);
        super.onStop();
        
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
    	
    }
	
	public void onSensorChanged(SensorEvent event) {
		synchronized(this) {
			if (event.sensor == accelerometer) {
				xViewA.setText(event.sensor.getName() + " x: " + event.values[0]);
				yViewA.setText(event.sensor.getName() + " y: " + event.values[1]);
				zViewA.setText(event.sensor.getName() + " z: " + event.values[2]);
			}
			if (event.sensor == compass) {
				xViewC.setText(event.sensor.getName() + " x: " + event.values[0]);
				yViewC.setText(event.sensor.getName() + " y: " + event.values[1]);
				zViewC.setText(event.sensor.getName() + " z: " + event.values[2]);
			}
			if (event.sensor == light) {
				vLight.setText(event.sensor.getName() + " x: " + event.values[0]);
			}
			if (event.sensor == orientation) {
				xViewO.setText(event.sensor.getName() + " x: " + event.values[0]);
				yViewO.setText(event.sensor.getName() + " y: " + event.values[1]);
				zViewO.setText(event.sensor.getName() + " z: " + event.values[2]);
			}
			
		}
		
	}
}